package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.CucheckDao;
import cn.gson.springboot.model.dao.afterdao.EvectionDao;
import cn.gson.springboot.model.dao.afterdao.EvectionRecordDao;
import cn.gson.springboot.model.mapper.aftermapper.EvectionMapper;
import cn.gson.springboot.model.pjos.Cucheck;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Evection;
import cn.gson.springboot.model.pjos.EvectionRecord;
import cn.gson.springboot.model.vo.aftervo.EvectionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EvectionService {

    @Autowired
    EvectionDao dao;
    @Autowired
    EvectionRecordDao recordDao;
    @Autowired
    EvectionMapper mapper;
    @Autowired
    CucheckDao cucheckDao;

    //出差表分页查询
    public List<Evection> selectEvetPage(Integer employeesId, Integer fatherDeptId, String data, String date, String sites){
        String datas = null;
        String dates = null;
        Date time;
        Date times;
        if(data != null){
            data = data.replace("Z", " UTC");
        }
        if(date != null){
            date = date.replace("Z", " UTC");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if(data != null){
                time = format.parse(data);
                String result = defaultFormat.format(time);
                datas = result;
                //System.out.println("开始"+datas);
            }
            if(date != null) {
                times = format.parse(date);
                String results = defaultFormat.format(times);
                dates = results;
                //System.out.println("结束"+dates);
            }
        } catch (Exception e) {
            time = null;
            times = null;
        }
        return mapper.evetPage(employeesId, fatherDeptId, datas, dates, sites);
    }

    //新增出差表
    public void addEvet(EvectionVo evet){
        Evection evets = new Evection();
        evets.setEmpByEmployeesId(evet.getEmpByEmployeesId());//申请人
        evets.setEvetId(evet.getEvetId());
        evets.setEvetDate(evet.getEvetDate());
        evets.setEvetData(evet.getEvetData());
        evets.setEvetDepart(evet.getEvetDepart());
        evets.setEvetGoal(evet.getEvetGoal());
        evets.setEvetSubject(evet.getEvetSubject());
        evets.setEvetTraffic(evet.getEvetTraffic());
        evets.setEvetRemark(evet.getEvetRemark());
        evets.setCustomerByCustomId(evet.getCustomerByCustomId());
        evets.setEvetEmpid(evet.getEvetEmpid());//审批人
        if(evet.getEvetState() == "" || evet.getEvetState() != null){
            evets.setEvetState("未出差");
        }else {
            evets.setEvetState(evet.getEvetState());
        }
        evets.setEvetLoan(evet.getEvetLoan());
        if(evet.getEvetStatus() == "" || evet.getEvetStatus() == null){
            evets.setEvetStatus("待审批");
        }else{
            evets.setEvetStatus(evet.getEvetStatus());
        }
        evets.setEvetDelete(1);
        dao.save(evets);
    }

    //逻辑删除
    public void deleteByEvetId(Integer apprId, Integer evetId){
        if(evetId != null){
            Evection evet = dao.findById(evetId).get();
            evet.setEvetDelete(0);
            dao.save(evet);
        }
        if(apprId != null){
            Cucheck cucheck = cucheckDao.findById(apprId).get();
            cucheck.setApprDelete(0);
            cucheckDao.save(cucheck);
        }
    }

    //回显出差表
    public Evection selectEvet(Integer evetIds){
        return mapper.evetAll(evetIds);
    }

    //新增出差日志
    public void addRecord(String substance, Integer employeesId, Integer evetId){
        EvectionRecord cord = new EvectionRecord();
        Emp emp = new Emp();
        Timestamp time = new Timestamp(new Date().getTime());
        emp.setEmployeesId(employeesId);
        cord.setEvetId(evetId);
        cord.setCordDate(time);
        cord.setCordContent(substance);
        cord.setEmpByEmployeesId(emp);
        recordDao.save(cord);
    }

    //回显出差日志表
    public List<EvectionRecord> selectCordEvetId(Integer evetId){
        return mapper.cordByEvetId(evetId);
    }

    //出差完成
    public void complete(Integer evetId){
        Evection evet = dao.findById(evetId).get();
        evet.setEvetState("出差完成");
        dao.save(evet);
    }
}
